package com.bsj.pay.controller.admin;

import com.bsj.pay.common.Result;
import com.bsj.pay.model.PayGoodsBean;
import com.bsj.pay.service.LogService;
import com.bsj.pay.service.PayGoodsService;
import com.bsj.pay.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller("payPoolController")
@RequestMapping("/admin/paypool")
public class PayPoolController {

    private static final Logger log = LogManager.getLogger(PayPoolController.class);

    @Autowired
    private PayGoodsService payGoodsService;

    @Autowired
    private LogService logService;

    @ResponseBody
    @RequestMapping(value = "/list.html")
    public Map list(HttpServletRequest request, HttpServletResponse res, int page, int limit, PayGoodsBean payGoods) {
        PayGoodsBean queryModel = new PayGoodsBean();
        if (!StringUtils.isEmpty(payGoods.getAmount())) queryModel.setAmount(payGoods.getAmount());
        if (!StringUtils.isEmpty(payGoods.getPay_type())) queryModel.setPay_type(payGoods.getPay_type());
        if (payGoods.getStatus() != null) queryModel.setStatus(payGoods.getStatus());
        PageInfo<PayGoodsBean> pageInfo = payGoodsService.pageList(queryModel, page, limit);
        return ResultUtil.getQuerySuccessResult(pageInfo);
    }


    @RequestMapping("/listPage.html")
    public ModelAndView listPage(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("admin/paypool/list");
        return mav;
    }

    @RequestMapping("/addPage.html")
    public ModelAndView addPage(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("admin/paypool/add");
        return mav;
    }

    @ResponseBody
    @RequestMapping("/remove.html")
    public Result remove(HttpServletRequest request, HttpServletResponse res, Long id) {
        Result result = new Result();
        try {
            if (payGoodsService.removeById(id) > 0) {
                result.setCode(0);
                result.setMsg("删除成功");
            } else {
                result.setCode(1);
                result.setMsg("删除失败");
            }
            return result;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setCode(500);
            result.setMsg(e.getMessage());
            return result;
        }

    }


    @ResponseBody
    @RequestMapping("/add.html")
    public Result add(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, PayGoodsBean payGoods) {
        Result result = new Result();
        try {

            PayGoodsBean queryModel = new PayGoodsBean();
            queryModel.setAmount(payGoods.getAmount());
            queryModel.setPay_type(payGoods.getPay_type());
            if (payGoodsService.count(queryModel) > 0) {
                result.setCode(1);
                if ("wechat".equalsIgnoreCase(payGoods.getPay_type())) {
                    result.setMsg("微信的金额已存在，添加失败!");
                } else if ("alipay".equalsIgnoreCase(payGoods.getPay_type())) {
                    result.setMsg("支付宝的金额已存在，添加失败!");
                }
                return result;
            }

            payGoods.setStatus(1);

            payGoods.setCreate_time(System.currentTimeMillis() / 1000);
            payGoodsService.insert(payGoods);

            logService.addLog(request, "新增金额" + payGoods.getAmount() + " 产码数量: " + payGoods.getPay_url_count());

            result.setCode(0);
            result.setMsg("添加成功");
            return result;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setCode(1);
            result.setMsg(e.getMessage());
            return result;
        }

    }


    @RequestMapping("/updatePage.html")
    public ModelAndView updatePage(HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelAndView mav, Long id) {
        mav.addObject("paypool", payGoodsService.getById(id));
        mav.setViewName("admin/paypool/update");
        return mav;
    }


    @ResponseBody
    @RequestMapping("/updateStatus.html")
    public Result updateStatus(HttpServletRequest request, HttpServletResponse res, Long id, Integer status) {
        Result result = new Result();
        try {
            PayGoodsBean payGoods = new PayGoodsBean();
            payGoods.setId(id);
            payGoods.setStatus(status == 0 ? 1 : 0);
            if (payGoodsService.update(payGoods) > 0) {
                result.setCode(0);
                result.setMsg("修改成功");
            } else {
                result.setCode(1);
                result.setMsg("修改失败");
            }
            return result;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setCode(500);
            result.setMsg(e.getMessage());
            return result;
        }

    }

    @ResponseBody
    @RequestMapping("/update.html")
    public Result update(HttpServletRequest request, HttpServletResponse res, PayGoodsBean payGoods) {
        Result result = new Result();
        try {
            payGoods.setUpdate_time(System.currentTimeMillis() / 1000);
            payGoodsService.update(payGoods);
            result.setCode(0);
            result.setMsg("修改成功");
            return result;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setCode(500);
            result.setMsg(e.getMessage());
            return result;
        }

    }
}
